define(["blockfactory", "level"],function(BlockFactory, Level){
   
    return function(levelArray, spriteBuffer){
      
        var levels = levelArray;
        var blockFactory = new BlockFactory(spriteBuffer);
        var blocks = [];
        
        this.buildLevel = function (numLevel){
            blocks.length = 0; // clear array! YES!
            var posX = 0;
            var posY = 0;
            var maxRowHeight = 0;
            var level = levels[numLevel-1];
            for(var i=0; i< level.rows.length; ++i){
                var row = level.rows[i];
                for( var j=0; j<row.length; ++j){
                    var block = blockFactory.createBlock(row[j]);
                    block.x = posX;
                    block.y = posY;
                    posX += block.w;
                    maxRowHeight = maxRowHeight < block.h ? block.h : maxRowHeight;            
                    blocks.push(block);
                } // end for j
                posY += maxRowHeight;
                posX = 0;
            } // end for i       
            return new Level(blocks, numLevel, level.bg);
        };
    };
    
});